package com.yang.drawcard.video.player;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.core.ui.loader.LatteLoader;
import com.yang.drawcard.R;
import com.yang.drawcard.application.ConstValues;
import com.yang.drawcard.base.BaseActivity;
import com.yang.drawcard.utils.ViewUtil;
import com.yang.drawcard.video.domain.VideoStc;

import java.lang.ref.SoftReference;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.app.ActivityCompat;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;


/**
 * PlayerActivity
 */
public class PlayerActivity extends BaseActivity implements View.OnClickListener {

    public static final String TAG = PlayerActivity.class.getSimpleName();

    private RelativeLayout backBtn;
    private RelativeLayout confirmBtn;
    private AppCompatTextView confirmTv;
    private AppCompatTextView backTv;
    private AppCompatTextView titleTv;
    private JzvdStdSpeed jzvideo;

    private VideoStc videoStc;

    private AppCompatActivity _mActivity;
    MyHandler handler;

    /**
     * 接收子线程消息的 Handler
     */
    public static class MyHandler extends Handler {

        // 软引用
        SoftReference<PlayerActivity> fragmentRef;

        public MyHandler(PlayerActivity fragment) {
            fragmentRef = new SoftReference<PlayerActivity>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            PlayerActivity fragment = fragmentRef.get();
            if (fragment == null) {
                return;
            }
            Bundle bundle = msg.getData();

            // 处理UI 变化
            switch (msg.what) {
                case ConstValues.WAIT0:
                    // fragment.getTermInfoSuc(bundle);
                    break;
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        // 标题栏白底黑字
        // Eyes.setStatusBarLightMode(this, Color.WHITE);
        //Eyes.setStatusBarLightMode(this, R.color.color_00C087);
        // 沉浸式
        setStatusBar();

        // 接收传递过来的参数
        initCreate();
        initView();
        initData();

    }

    private void initCreate() {

        /*// 上一页面设置数据
        Intent intent = null;
        intent = new Intent(this, RePwdActivity.class);
        Bundle bundl = new Bundle();
        intent.putExtra("username", "");
        intent.putExtra("password", "");
        startActivity(intent);*/

        // 本页面获取数据
        Intent i = getIntent();
        videoStc = (VideoStc) i.getSerializableExtra("videoStc");
    }

    private void initView() {

        backBtn = (RelativeLayout) findViewById(R.id.top_dd_navigation_rl_back);
        confirmBtn = (RelativeLayout) findViewById(R.id.top_dd_navigation_rl_confirm);
        confirmTv = (AppCompatTextView) findViewById(R.id.top_dd_navigation_bt_confirm);
        backTv = (AppCompatTextView) findViewById(R.id.top_dd_navigation_bt_back);
        titleTv = (AppCompatTextView) findViewById(R.id.top_dd_navigation_tv_title);

        confirmBtn.setVisibility(View.INVISIBLE);
        //confirmBtn.setOnClickListener(this);
        backBtn.setOnClickListener(this);


        jzvideo = (JzvdStdSpeed) findViewById(R.id.jz_video);

    }

    private void initData() {
        LatteLoader.stopLoading();
        handler = new MyHandler(this);
        _mActivity = PlayerActivity.this;
        titleTv.setText(videoStc.getTitle());

        jzvideo.setUp(videoStc.getMp4url(), videoStc.getTitle());
        Glide.with(this).load(videoStc.getImageurl()).into(jzvideo.posterImageView);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 返回
            case R.id.top_dd_navigation_rl_back:
                if (ViewUtil.isDoubleClick(v.getId(), 1000))
                    return;
                this.finish();
                break;
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }

    // 对返回键进行监听
    @Override
    public void onBackPressedSupport() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            pop();
        } else if (Jzvd.backPress()) {
            return;
        } else {
            ActivityCompat.finishAfterTransition(this);
        }
    }
}
